package com.jy.modules.cims.component.image.service;

import java.util.List;
import java.util.Observable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.component.image.TransferImagesDto;
import com.jy.modules.cims.component.tbj.TBJBusinessClient;
import com.jy.modules.cims.data.dao.JjDao;
import com.jy.modules.cims.data.dto.inter.tbj.TBJMessage;
import com.jy.modules.cims.data.dto.inter.tbj.TBJResponse;
import com.jy.modules.cims.data.dto.inter.tbj.jj.BankCardDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.BorrowerDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.ContactDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.CreditInfoDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.EntityDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.IncomeDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.InsuranceDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.JjDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.JobsDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.LoanContentDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.LoanInfoDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.MechanismQueryNumberDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.RepayInfoDTO;
import com.jy.modules.cims.data.dto.inter.tbj.jj.ThirdDataDTO;
import com.tongbanjie.open.api.enums.EntryResponseEnum;

/**
 * @description 发送借款人信息服务
 * @author zhangmi
 * @date 2017年2月8日
 */
@Component
public class SendBorrowerInfoService extends Observable {
	private static Logger logger = LoggerFactory.getLogger(SendBorrowerInfoService.class);
	
	@Autowired
	private SpringContextUtils springContextUtils;
	
    @Autowired
    @Qualifier("com.jy.modules.cims.component.tbj.TBJBusinessClient")
    private TBJBusinessClient tbj;

    @Autowired
    private JjDao jjDao;
	/**
	 * 铜板街发送进件信息
	 * @author zhangmi
	 * @date  2017年2月8日
	 * @param transferImagesDto
	 */
	public void doSomething(TransferImagesDto transferImagesDto) {
		String contractNo = transferImagesDto.getContractNo();
		try{
			
			logger.debug("发送铜板街进件信息");
	        List<LoanContentDTO> list = jjDao.findLoanContractNo(contractNo);
	        if(CollectionsUtils.isEmpty(list)){
	        	logger.info("发送铜板街进件信息》查询数据为空！");
				return;
	        }
	        for (LoanContentDTO loanDTO : list) {
	        	   JjDTO jjDTO = new JjDTO();
	               jjDTO.setLoan(loanDTO);
	               // 补充进件信息
	               BorrowerDTO borrower = jjDao.findBorrower(loanDTO);
	               List<JobsDTO> jobs = jjDao.findBorrowerJobs(loanDTO);
	               ThirdDataDTO thirdData = jjDao.findBorrowerThirdData(loanDTO);
	               List<ContactDTO> contact = jjDao.findBorrowerAtten(loanDTO);
	               List<BankCardDTO> bankCard = jjDao.findBorrowerBankCard(loanDTO);
	               List<EntityDTO> entity = jjDao.findBorrowerEntity(loanDTO);
	               List<InsuranceDTO> insurance = jjDao.findBorrowerInsurance(loanDTO);
	               LoanInfoDTO loanInfo = jjDao.findLoanInfo(loanDTO);
	               CreditInfoDTO creditInfo = jjDao.findCreditInfo(loanDTO);
	               RepayInfoDTO repayInfoInfo = jjDao.findRepayInfo(loanDTO);
	               IncomeDTO income = jjDao.findIncome(loanDTO);
	               MechanismQueryNumberDTO mechanismQueryNumber = jjDao.findMechanismQueryNumber(loanDTO);
	               jjDTO.setBorrower(borrower);// 借款人基本信息
	               jjDTO.setJobs(jobs);// 工作
	               jjDTO.setThirdData(thirdData);// 第三方测算数据
	               jjDTO.setContact(contact);// 联系人
	               jjDTO.setBankCard(bankCard);// 银行卡
	               jjDTO.setEntity(entity);// 经营实体
	               jjDTO.setInsurance(insurance);// 投保信息
	               //if(null != loanInfo){
	               //	loanInfo.setLoanBank(null);
	               //	loanInfo.setLoanBankNum(null);
	              // }
	               jjDTO.setLoanInfo(loanInfo);// 放款信息
	               jjDTO.setCreditInfo(creditInfo);
	               jjDTO.setRepayInfo(repayInfoInfo);//还款信息
	               jjDTO.setIncome(income);//借款人收入信息
	               jjDTO.setMechanismQueryNumber(mechanismQueryNumber);//机构查询次数
	               //
	               TBJResponse tBJResponse = tbj.addConsume(jjDTO);
	               if (tBJResponse.getRetCode().equals(EntryResponseEnum.SUCESS21001.getCode())) {
	                   String res = ((TBJMessage) tBJResponse.getData()).getResult();
	                   if (!"2".equals(res)) {// 没有失败的更新本地状态
	                       jjDao.updateLoan(loanDTO);
	                       logger.info("发送铜板街进件信息成功" + loanDTO.getLoanNumber());
	                   } else {
	                       logger.info("发送铜板街进件信息失败" + ((TBJMessage) tBJResponse.getData()).getMessage());
	                       logger.info("发送铜板街进件信息失败" + loanDTO.getLoanNumber());
	                   }
	               } else {
	                   logger.info("发送铜板街进件信息失败" + loanDTO.getLoanNumber());
	               }
			 }
		} catch (Exception e) {
			logger.error("合同号："+contractNo+"进件信息发送异常："+e.getMessage());
		} 
	}
}
